// count 的 reducer 函数
let reducer = (state=100, action) => {  
  //对 action type 属性进行判断
  switch(action.type){
    case 'add':
      return state + action.payload;
    case 'sub':
      return state - action.payload;
    //该回调函数会同步调用一次, 通过返回值获得内部的状态初始值, 该行代码一定要写
    default: 
      return state;
  }
};

export default reducer;


//创建 action creator 创造者
export function addAction(payload){
  return {
    type: 'add',
    payload: payload
  }
}

//减法的 action creator
export let subAction = payload => ({type: 'sub', payload});

//封装函数返回回调函数 
// 异步的 action creator
export let asyncAddAction = (payload) => {
  return dispatch => {
    setTimeout(() => {
      dispatch({type: 'add', payload: payload});
    }, 1000);
  }
}
